package com.maishi.programmer.jainzhioffer;

import java.util.HashMap;

/**
 * Created by brossdong on 2018/4/12.
 * 输入两个链表，找出它们的第一个公共结点。
 */
public class TestProblem52 {

    public class ListNode {
        int val;
        ListNode next = null;
        ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode FindFirstCommonNode(ListNode pHead1, ListNode pHead2) {

        ListNode current1 = pHead1;
        ListNode current2 = pHead2;

        HashMap<ListNode,Integer> hashMap = new HashMap<>();
        while (current1 != null) {
            hashMap.put(current1,null);
            current1 = current1.next;
        }
        while (current2 != null) {
            if (hashMap.containsKey(current2))
                return current2;
            current2 = current2.next;
        }
        return null;
    }

}
